package cn.demomaster.quickui.view.tabmenu;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.demomaster.quickcompat.view.adapter.QuickRecyclerAdapter;

import java.util.ArrayList;
import java.util.List;

import cn.demomaster.quickui.R;


/**
 * @author squirrel桓
 * @date 2018/11/9.
 * description：
 */
// ① 创建Adapter
public class TabMenuAdapter extends QuickRecyclerAdapter<TabMenuAdapter.ViewHolder> {

    private List<TabListViewItem> tabListViewItems = new ArrayList();
    private int tabIndex;
    private int selectCount = 1;
    private Context context;
    private LayoutInflater inflater;
    private boolean isSingle;//单个tab中的状态集合（多选则为list,单选则为int）

    public TabMenuAdapter(Context context, int tabIndex) {
        this.context = context;
        this.tabIndex = tabIndex;
        //判断单选还是多选
        selectCount = 1;
        isSingle = (selectCount == 1);

        //遍历循环初始化viewData
        tabListViewItems.clear();
        this.inflater = ((Activity) context).getLayoutInflater();
    }

    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.quick_item_tab_menu, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, final int position) {
        holder.tv_title.setSelected(tabListViewItems.get(position).isSelected());
        holder.tv_title.setText(this.tabListViewItems.get(position).getItemName());
    }

    @Override
    public int getItemCount() {
        return this.tabListViewItems.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        private TextView tv_title;
        public ViewHolder(View itemView) {
            super(itemView);
            tv_title = itemView.findViewById(R.id.tv_title);
        }
    }

   /* private OnItemClickListener onItemClickListener;

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public interface OnItemClickListener {
        void onItemClick(View view, int position);
    }*/


    public void setOnItemClicked(int position) {
        boolean b = !tabListViewItems.get(position).isSelected();
        List<Integer> current = new ArrayList<>();
        if (current.contains(position)) {//如果存在则remove
            current.remove((Object) position);
        } else {//如果不存在
//            if (current.size() + 1 > tabMenuModel.getSelectCount()) {//判断个数是否超出，没超出则追加，超出则remove第一个
//                if(current.get(0)>=0) {
//                    tabListViewItems.get(current.get(0)).setSelected(false);
//                }
//                current.remove(0);
//                current.add(position);
//            } else {
//                current.add(position);
//            }
        }
        tabListViewItems.get(position).setSelected(b);
        notifyDataSetChanged();
    }
}